<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Storage;
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $img = DB::table('images')->get();
        return view('home.image_index')->with('img',$img);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        // return view('home.image_add');
        return view('home.ajax_image_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        // dd($request->file('img'));
        // $img = $request->file('img');

        // if($img->isValid()){
        //     $ext = $img->getClientOriginalExtension();
        //     $path = public_path('upload/images/');
        //     $name = 'origin_'.time().rand(10,100).'.'.$ext;
        //     if($img->move($path,$name)){
        //         $url = '/upload/images/'.$name;
        //         DB::table('images')->insert(['url'=>$url]);
        //         echo $url;
        //     }
        // }
        // $imgs = $request->file('img');
        // $urls = [];
        // foreach($imgs as $img){

        //     if($img->isValid()){
        //         $ext = $img->getClientOriginalExtension();
        //         $path = public_path('upload/images/');
        //         $name = 'origin_'.time().rand(10,100).'.'.$ext;
        //         if($img->move($path,$name)){
        //             $url = '/upload/images/'.$name;
        //             $urls[]['url']=$url;

                    
        //         }
        //     }
        // }
        // DB::table('images')->insert($urls);
        // return redirect('image');
//文件存储
        $imgs = $request->file('img');
        $urls = [];
        foreach($imgs as $img){

            if($img->isValid()){
                $ext = $img->getClientOriginalExtension();
                $path = 'images/';
                $name = 'origin_'.time().rand(10,100).'.'.$ext;
                $path .= $name;
                $tmp=$img->getRealPath();
                Storage::disk('myupload')->put($path,file_get_contents($tmp));
                $urls[]['url']='/upload/'.$path;

            }
        }
        DB::table('images')->insert($urls);
        // return redirect('image');
        // $data =json_encode($urls);
        $data = [
            'status'=>200,
            'msg'=>'成功',
            'data'=>$urls,
        ];
        echo json_encode($data);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
